using System;
using System.Collections.Generic;

namespace ShadeTree.Binding.Lists
{
    public class FancyPicklist : Picklist<ListValue>
    {
        public FancyPicklist() : base(new ListValue[0])
        {
            configure();
        }

        public FancyPicklist(List<ListValue> items) : base(items)
        {
            configure();
        }

        public FancyPicklist(ListValue[] items) : base(items)
        {
            configure();
        }

        private void configure()
        {
            ValueMember = "Value";
            DisplayMember = "Display";
        }

        public object GetValueForDisplay(string display)
        {
            ListValue listValue =
                _items.Find(delegate(ListValue v) { return v.Display.ToUpper() == display.ToUpper(); });

            if (listValue == null)
            {
                throw new ApplicationException("Could not find a Column with heading " + display);
            }

            return listValue.Value;
        }

        public void AddValue(string display, string innerValue)
        {
            var listValue = new ListValue(display, innerValue);
            _items.Add(listValue);
        }

        public void SetDefault(string display, string innerValue)
        {
            var listValue = new ListValue(display, innerValue);
            _items.Insert(0, listValue);
        }
    }
}